#include "hikari.h"

HObj::HObj(ISceneNode*node){
	this->node=node;
	animationData=NULL;
	updateable=false;
}


HObj::~HObj(){
	node->removeAll();
	node->remove();
}

hComponent*HObj::getComponent(wstring&componentName){
	int count=components.size();
	for(int i=0;i<count;i++){
		if(Utils::equal(componentName, components[i]->getName())){
			return components[i];
		}
	}
	return NULL;
}

void HObj::addComponent(hComponent*component){
	if(component==NULL || Utils::isEmpty(component->getName())){
		Utils::errorLog(L"Try to add empty component to object");
		return;
	}
	if(getComponent(component->getName())!=NULL){
		wstring message=wstring(L"WARNING: Object [")+getName()+L"] already has component with name ["+component->getName()+L"]";
		Utils::errorLog(message);
	}
	components.push_back(component);
}

vector<hComponent*>& HObj::getComponentList(){
	return components;
}

bool HObj::enableComponent(wstring&componentName, bool enable){
	hComponent*component=getComponent(componentName);
	if(component==NULL){
		wstring message=wstring(L"WARNING: Object [")+getName()+L"] does not have component ["+component->getName()+L"] to enable it";
		Utils::errorLog(message);
		return false;
	}
	component->setEnabled(enable);
	return true;
}

void HObj::setPosition(vector3df position){
	node->setPosition(position);
}

void HObj::setRotation(vector3df rotation){
	node->setRotation(rotation);
}

void HObj::setScale(vector3df scale){
	node->setScale(scale);
}

vector3df HObj::getPosition(){
	return node->getPosition();
}

vector3df HObj::getRotation(){
	return node->getRotation();
}

vector3df HObj::getScale(){
	return node->getScale();
}

void HObj::move(vector3df offset){
}

void HObj::moveX(float offset){
	vector3df pos=node->getPosition()+node->getRotation().rotationToDirection(vector3df(offset, 0, 0));
	node->setPosition(pos);
}

void HObj::moveY(float offset){
	vector3df pos=node->getPosition()+node->getRotation().rotationToDirection(vector3df(0, offset, 0));
	node->setPosition(pos);
}

void HObj::moveZ(float offset){
	vector3df origPos=node->getPosition();
	vector3df rotation=node->getRotation();
	vector3df rotationPos=rotation.rotationToDirection(vector3df(0, 0, offset));
	vector3df pos=origPos+rotationPos;
	node->setPosition(pos);
}

void HObj::animate(wstring&animationTrackName){
	IAnimatedMeshSceneNode*tnode=dynamic_cast<IAnimatedMeshSceneNode*>(node);
	if(tnode==NULL){
		Utils::errorLog(wstring(L"3dObject name[")+getName()+L"] is not animated object");
		return;
	}
	AnimationTrack*track=animationData->getAnimationTrack(animationTrackName);
	if(track==NULL){
		Utils::errorLog(wstring(L"3dObject name[")+getName()+L"] does not contain animation track ["+animationTrackName+L"%s]");
		return;
	}

	tnode->setAnimationSpeed(track->getSpeed());
	tnode->setCurrentFrame((f32)track->getStartFrame());
	tnode->setFrameLoop(track->getStartFrame(), track->getEndFrame());
}